package RevolCraft.TileEntities;

import cpw.mods.fml.common.registry.GameRegistry;
import RevolCraft.Interfaces.IElectric;
import net.minecraft.src.Block;
import net.minecraft.src.EntityPlayer;
import net.minecraft.src.IInventory;
import net.minecraft.src.INetworkManager;
import net.minecraft.src.Item;
import net.minecraft.src.ItemBlock;
import net.minecraft.src.ItemHoe;
import net.minecraft.src.ItemStack;
import net.minecraft.src.ItemSword;
import net.minecraft.src.ItemTool;
import net.minecraft.src.Material;
import net.minecraft.src.NBTTagCompound;
import net.minecraft.src.NBTTagList;
import net.minecraft.src.Packet132TileEntityData;
import net.minecraft.src.TileEntity;
import net.minecraft.src.TileEntityRenderer;

public class TileEntityGenerator extends TileEntity implements IInventory, IElectric {
	private ItemStack[] inv;
	public int maxStorage;
	public int currentStorage;
	public int burnTime;
	public int currentItemBurnTime;
	
	public TileEntityGenerator()
	{
		inv = new ItemStack[2];
		maxStorage = 10000;
		currentStorage = 0;
		burnTime = 0;
		currentItemBurnTime = -1;
	}
	
	public void updateEntity()
	{
		if(burnTime > 0)
		{
			--burnTime;
			currentStorage += 3;
			if(currentStorage > maxStorage)
				currentStorage = maxStorage;
		}
		else
		{
			if(inv[1] != null)
			{
				int temp = getItemBurnTime(inv[1]);
				if(temp != 0)
				{
					decrStackSize(1, 1);
					burnTime = temp;
					currentItemBurnTime = temp;
				}
			}
		}
		//System.out.println(currentStorage);
		//this.worldObj.markBlockNeedsUpdate(xCoord, yCoord, zCoord);
	}

	@Override
	public int getSizeInventory() {
		return inv.length;
	}

	@Override
	public ItemStack getStackInSlot(int slot) {
		return inv[slot];
	}

	@Override
	public ItemStack decrStackSize(int slot, int amount) {
		ItemStack stack = getStackInSlot(slot);
        if (stack != null) {
                if (stack.stackSize <= amount) {
                        setInventorySlotContents(slot, null);
                } else {
                        stack = stack.splitStack(amount);
                        if (stack.stackSize == 0) {
                                setInventorySlotContents(slot, null);
                        }
                }
        }
        return stack;
	}

	@Override
    public ItemStack getStackInSlotOnClosing(int slot) {
            ItemStack stack = getStackInSlot(slot);
            if (stack != null) {
                    setInventorySlotContents(slot, null);
            }
            return stack;
    }

	@Override
    public void setInventorySlotContents(int slot, ItemStack stack) {
            inv[slot] = stack;
            if (stack != null && stack.stackSize > getInventoryStackLimit()) {
                    stack.stackSize = getInventoryStackLimit();
            }               
    }

	@Override
	public String getInvName() {
		return "Generator";
	}

	@Override
    public int getInventoryStackLimit() {
            return 64;
    }

	@Override
    public boolean isUseableByPlayer(EntityPlayer player) {
            return worldObj.getBlockTileEntity(xCoord, yCoord, zCoord) == this &&
            player.getDistanceSq(xCoord + 0.5, yCoord + 0.5, zCoord + 0.5) < 64;
    }

	@Override
	public void openChest() {
	}

	@Override
	public void closeChest() {
	}
	
	public void writeToNBT(NBTTagCompound par1NBTTagCompound)
	{
		super.writeToNBT(par1NBTTagCompound);
		NBTTagList lista = new NBTTagList();
		for(int it = 0; it < this.inv.length; it++)
		{
			if (this.inv[it] != null)
            {
				NBTTagCompound przedmiot = new NBTTagCompound();
				przedmiot.setByte("Slot", (byte)it);
				this.inv[it].writeToNBT(przedmiot);
				lista.appendTag(przedmiot);
            }
		}
		
		par1NBTTagCompound.setTag("Items", lista);
		
		par1NBTTagCompound.setInteger("maxStorage", maxStorage);
		par1NBTTagCompound.setInteger("currentStorage", currentStorage);
		par1NBTTagCompound.setInteger("burnTime", burnTime);
		par1NBTTagCompound.setInteger("currentItemBurnTime", currentItemBurnTime);
	}
	
	public void readFromNBT(NBTTagCompound par1NBTTagCompound)
	{
		super.readFromNBT(par1NBTTagCompound);
        NBTTagList lista = par1NBTTagCompound.getTagList("Items");
        this.inv = new ItemStack[this.getSizeInventory()];

        for (int it = 0; it < lista.tagCount(); ++it)
        {
            NBTTagCompound przedmiot = (NBTTagCompound)lista.tagAt(it);
            int slot = przedmiot.getByte("Slot") & 255;

            if (slot >= 0 && slot < this.inv.length)
            {
                this.inv[slot] = ItemStack.loadItemStackFromNBT(przedmiot);
            }
        }
        
        maxStorage = par1NBTTagCompound.getInteger("maxStorage");
        currentStorage = par1NBTTagCompound.getInteger("currentStorage");
        burnTime = par1NBTTagCompound.getInteger("burnTime");
        currentItemBurnTime = par1NBTTagCompound.getInteger("currentItemBurnTime");
        
        /*System.out.println("IB: "+par1NBTTagCompound.getInteger("currentItemBurnTime"));
        System.out.println("ms: "+par1NBTTagCompound.getInteger("maxStorage"));
        System.out.println("cs: "+par1NBTTagCompound.getInteger("currentStorage"));
        System.out.println("bt: "+par1NBTTagCompound.getInteger("burnTime"));
        System.out.println("IB: "+currentItemBurnTime);
        System.out.println("ms: "+maxStorage);
        System.out.println("cs: "+currentStorage);
        System.out.println("bt: "+burnTime);*/
	}

	@Override
	public int getMaxStorage() {
		return maxStorage;
	}

	@Override
	public int getStorageLevel() {
		return currentStorage;
	}

	@Override
	public int takeAmount(int amount) {
		if(amount > currentStorage)
		{
			int temp = currentStorage;
			currentStorage = 0;
			return temp;
		}
		else
		{
			currentStorage -= amount;
			return amount;
		}
	}

	@Override
	public boolean isSource() {
		return true;
	}
	
	public static int getItemBurnTime(ItemStack par0ItemStack)
    {
        if (par0ItemStack == null)
        {
            return 0;
        }
        else
        {
            int var1 = par0ItemStack.getItem().shiftedIndex;
            Item var2 = par0ItemStack.getItem();

            if (par0ItemStack.getItem() instanceof ItemBlock && Block.blocksList[var1] != null)
            {
                Block var3 = Block.blocksList[var1];

                if (var3 == Block.woodSingleSlab)
                {
                    return 150;
                }

                if (var3.blockMaterial == Material.wood)
                {
                    return 300;
                }
            }
            if (var2 instanceof ItemTool && ((ItemTool) var2).func_77861_e().equals("WOOD")) return 200;
            if (var2 instanceof ItemSword && ((ItemSword) var2).func_77825_f().equals("WOOD")) return 200;
            if (var2 instanceof ItemHoe && ((ItemHoe) var2).func_77842_f().equals("WOOD")) return 200;
            if (var1 == Item.stick.shiftedIndex) return 100;
            if (var1 == Item.coal.shiftedIndex) return 1600;
            if (var1 == Item.bucketLava.shiftedIndex) return 20000;
            if (var1 == Block.sapling.blockID) return 100;
            if (var1 == Item.blazeRod.shiftedIndex) return 2400;
            return GameRegistry.getFuelValue(par0ItemStack);
        }
    }
	
	public int getBurnTimeRemainingScaled(int scale)
    {
		return burnTime * scale / currentItemBurnTime;
    }
	
	public int getStorageLevelScaled(int scale)
	{
		return currentStorage * scale / maxStorage;
	}
}
